﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LuxDiner.Model.Orders;
using LuxDiner.Model.Members;
using LuxDiner.Model.Financial;
using Utils;
namespace LuxDiner.Command.Orders
{
    public class PaidOrderCommand
    {
        public static PaidOrderModel NewOrder(PaidOrderModel order)
        {
            if (order != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var orderMethod = (OrderMethod)Enum.Parse(typeof(OrderMethod), order.Method.ToString());
                    order.OrderNo = Comm.Orders.CreateOrderNo(db, orderMethod);

                    var dOrder = Convertor.ConvertFrom<order_paid, PaidOrderModel>(order);
                    var dItems = Convertor.ConvertFromCollection<order_paid_details, PaidOrderDetailModel>(order.Items);
                    foreach (var it in dItems)
                        dOrder.order_paid_details.Add(it);
                    db.DataContext.order_paid.Add(dOrder);

                    #region 插入费用表
                    order.TotalMoney *= -1;
                    var payBalance = order.TotalMoney;
                    var pay = db.DataContext.payments.Where(p => p.CustomerID == order.CustomerID).OrderByDescending(p => p.ID).FirstOrDefault();
                    if (pay != null)
                        payBalance = pay.Balance + order.TotalMoney;
                    db.DataContext.payments.Add(new payment()
                    {
                        CustomerID = order.CustomerID,
                        Money = order.TotalMoney,
                        Description = (order.Method == (int)Model.Orders.OrderMethod.FK) ? "付款单" : "收款单",
                        OrderNo = order.OrderNo,
                        NoteDate = order.NoteDate,
                        CreatedOn = DateTime.Now,
                        OperatorUserID = order.OperatorUserID,
                        Balance = payBalance,
                        Method = order.Method
                    });
                    #endregion

                    db.DataContext.SaveChanges();
                    order.ID = dOrder.ID;
                    return order;
                }
            }
            return null;
        }
        /// <summary>
        /// 查询单据列表
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static PaidOrderCollection GetOrders(PaidOrderQuery sea)
        {
            var result = new PaidOrderCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<order_paid> q = db.DataContext.order_paid;
                if (sea != null)
                {
                    if (!string.IsNullOrEmpty(sea.OrderNo))
                        q = q.Where(p => p.OrderNo == sea.OrderNo);
                    if (sea.CustomerID > 0)
                        q = q.Where(p => p.CustomerID == sea.CustomerID);
                    if (sea.OperatorUserID > 0)
                        q = q.Where(p => p.OperatorUserID == sea.OperatorUserID);
                    if (sea.OrderDateBefore != null)
                        q = q.Where(p => p.NoteDate.CompareTo(sea.OrderDateBefore.Value) >= 0);
                    if (sea.OrderDateAfter != null)
                        q = q.Where(p => p.NoteDate.CompareTo(sea.OrderDateAfter.Value) <= 0);
                    if (sea.Method > 0)
                        q = q.Where(p => p.Method == sea.Method);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<PaidOrderModel, order_paid, PaidOrderCollection>(lst);
                    result.TotalRecordsCount = n;
                    foreach (var m in result)
                    {
                        var d = lst.Find(p => p.OrderNo == m.OrderNo);
                        if (sea.GetCustomer)
                            m.Customer = Convertor.ConvertTo<CustomerModel, customer>(d.customer);
                        if (sea.GetItems)
                            m.Items = Convertor.ConvertCollection<PaidOrderDetailModel, order_paid_details, PaidOrderDetailCollection>(d.order_paid_details);
                    }
                }
            }
            return result;
        }

        public static PaidOrderModel GetOrder(string orderNo)
        {
            if (!string.IsNullOrEmpty(orderNo))
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.order_paid.FirstOrDefault(p => p.OrderNo == orderNo);
                    if (d != null)
                    {
                        var m = Convertor.ConvertTo<PaidOrderModel, order_paid>(d);
                        m.Customer = Convertor.ConvertTo<CustomerModel, customer>(d.customer);
                        m.Items = Convertor.ConvertCollection<PaidOrderDetailModel, order_paid_details, PaidOrderDetailCollection>(d.order_paid_details);
                        foreach (var o in m.Items)
                        {
                            var di = d.order_paid_details.FirstOrDefault(p => p.ID == o.ID);
                            o.Bank = Convertor.ConvertTo<BankModel, bank>(di.bank);
                        }
                        return m;
                    }
                }
            }
            return null;
        }
    }
}
